<?php
$myattachfolder = 'attach/upload/';
$prefix = '';
if ($_REQUEST['prefix']) {
	$prefix = $_REQUEST['prefix'];
}
$myattachfolder = $myattachfolder.$prefix;
$size = 0;
if (isset($_REQUEST['sizes'])) {
	$size = $_REQUEST['sizes'];
}

function replaceFileNameIfExist($savePath){
	$a= (string)(microtime(true)*1000);
	$b=split('\.',$a);
	$append= $b[0];

	$docPos = stripos($savePath, '.');
	$newPath = substr($savePath, 0, $docPos);
	return $newPath.$append.substr($savePath,$docPos);
}

function myUploadFile($myattachfolder,$filecontent,$savePath,$filename,$filetype,$filesize,$idx){
	if ($filesize==0) {
		$filesize = 1;
	}
	if (file_exists('upload/'.$savePath)) {
		myUploadFile($myattachfolder,$filecontent, replaceFileNameIfExist($savePath),$filename,$filetype,$filesize,$idx);
	}else{
		echo '<div style="padding:1px">
				<input type="checkbox" onclick="checkAttach(this)" style="margin:1px" checked="checked" filepath="'.$myattachfolder.$savePath.'" /><a href="attach/downloadattach.php?filepath='.$savePath.'&filename='.$filename.'&filetype='.$filetype.'" >'.$filename.' ('.$filetype.') '.$filesize.'k </a>
				<input type="hidden" id="flagattach" name="flagattach['.$idx.']" value="true"/>
				<input type="hidden" name="attachpath['.$idx.']" value="'.$myattachfolder.'"/><input type="hidden" name="attachreal['.$idx.']" value="'.$savePath.'"/>
				<input type="hidden" name="attachfile['.$idx.']" value="'.$filename.'"/><input type="hidden" name="attachtype['.$idx.']" value="'.$filetype.'"/>
				<input type="hidden" name="attachsize['.$idx.']" value="'.$filesize.'k"/><input type="hidden" name="attachid['.$idx.']" value="-1"/></div>';
		move_uploaded_file($filecontent,'upload/'.$savePath);
	}
}

function uploadSimpleFile($myattachfolder,$size){
	if ($_FILES["file"]["error"][$idx] > 0)
	{
		continue;
	}else{
		$preventfiles = array("php");
		$extension = end(explode(".", $_FILES["file"]["name"]));
		if (in_array($extension, $preventfiles)) {
			return;
		}else {
			myUploadFile($myattachfolder,$_FILES["file"]["tmp_name"],$_FILES["file"]["name"],$prefix.$_FILES["file"]["name"],$_FILES["file"]["type"],intval($_FILES["file"]["size"]/1024),$size + 0);
		}
	}
}

function uploadMultipleFiles($myattachfolder,$size){
	$preventfiles = array("php");
	foreach ($_FILES["file"]["name"] as $idx => $name){
		if ($_FILES["file"]["error"][$idx] > 0)
		{
			continue;
		}else{
			$extension = end(explode(".", $_FILES["file"]["name"][$idx]));
			if (in_array($extension, $preventfiles)) {
				continue;
			}else {
				myUploadFile($myattachfolder,$_FILES["file"]["tmp_name"][$idx],$_FILES["file"]["name"][$idx],$prefix.$_FILES["file"]["name"][$idx],$_FILES["file"]["type"][$idx],intval($_FILES["file"]["size"][$idx]/1024),$size + $idx);
			}
		}
	}
}

if (!isset($_FILES["file"]))
{
	echo "Error: file empty <br />";
}
else
{
	if (is_array($_FILES["file"]["tmp_name"])) {
		uploadMultipleFiles($myattachfolder,$size);
	}else{
		uploadSimpleFile($myattachfolder,$size);
	}

	/*echo "Upload: " . $_FILES["file"]["name"] . "<br />";
	 echo "Type: " . $_FILES["file"]["type"] . "<br />";
	 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
	 echo "Stored in: " . $_FILES["file"]["tmp_name"];
	 if (file_exists("upload/" . $_FILES["file"]["name"]))
	 {
		echo $_FILES["file"]["name"] . " already exists. ";
		}
		else
		{
		move_uploaded_file($_FILES["file"]["tmp_name"],
		"upload/" . $_FILES["file"]["name"]);
		echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
		}*/
}

?>